//
//  LSZStatusTableViewCell.swift
//  醒浪微博
//
//  Created by 雷顺哲 on 16/7/13.
//  Copyright © 2016年 LSZ. All rights reserved.
//

import UIKit
import SnapKit
/*
    分析出三大块视图:
        1.原创微博视图
        2.转发微博视图
        3.toolbar视图
*/
//首页cell的子控件的间距
let HomeCellMargin: CGFloat = 10

//微博首页的cell
class LSZStatusTableViewCell: UITableViewCell {
    
//    记录toobar的顶部约束
    var toobarTopConstraint: Constraint?
    
//    使用viewModel模型
    var statusViewModel: LSZStatusViewModel? {
        didSet {
            
//            给原创微博视图设置需要的视图模型
            originalView.statusViewModel = statusViewModel
            
//            卸载约束
            toobarTopConstraint?.uninstall()
            
//            判断是否有转发微博对象
            if statusViewModel?.status?.retweeted_status != nil {
                
//                有转发微博
                retweetView.statusViewModel = statusViewModel
                
                retweetView.hidden = false
                
                toolBar.snp_makeConstraints(closure: { (make) -> Void in
                    self.toobarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            }else {
//                没有转发微博,隐藏转发微博视图,更新约束
                retweetView.hidden = true
                
                toolBar.snp_makeConstraints(closure: { (make) -> Void in
                    self.toobarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            
            
//            给toolbar设置需要的视图模型
            toolBar.statusViewModel = statusViewModel
        }
    }
    
    
    
    
    
    // MARK:懒加载控件
//    原创微博视图
    private lazy var originalView: LSZStatusOriginalView = LSZStatusOriginalView()
//  toobar
    private lazy var toolBar: LSZStatusToolBar = LSZStatusToolBar()
//    转发微博视图
    private lazy var retweetView: LSZStatusRetweetView = LSZStatusRetweetView()
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpUI()
    }
   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
//    添加视图的约束
    private func setUpUI() {
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
//        添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBar)
        
        
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(8)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        toolBar.snp_makeConstraints { (make) -> Void in
            self.toobarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.height.equalTo(35)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        
//        确定cell的行高
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBar)
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
        }
        
    }
    
    
    
    
}
